﻿using IOP.Extension.DependencyInjection;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample112;

public class TransformSystem : ComponentSystem
{
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="contextManager"></param>
    public TransformSystem()
    {
    }

    public override void Initialization()
    {
        base.Initialization();
        ContextManager.Foreach<OBJGroup>((group) =>
        {
            Context context = ContextManager.GetContext(group.Entity);
            var render = context.GetContextRenderObject();
            var a = render.GetComponents<ITexture>().Where(x => x.Binding == 2).First();
            a.UpdateTextureMemoryData(0.9f);
        });
    }

    public override void Update(TimeSpan timeSpan)
    {
        ContextManager.Foreach<OBJGroup>((group) =>
        {
            Context context = ContextManager.GetContext(group.Entity);
            ref MVPMatrix mvp = ref context.GetMVPMatrix();
            var render = context.GetContextRenderObject();
            Transform transform = render.GetTransform();
            mvp.ModelMatrix = Matrix4x4.CreateTranslation(transform.Position) *
            Matrix4x4.CreateFromQuaternion(transform.Rotation) *
            Matrix4x4.CreateScale(transform.Scaling);
            var a = render.GetComponents<FudgeAnimation>().First();
            a.Play(timeSpan);
            context.PushToRenderGroup();
        });
    }
}

public struct OBJGroup
{
    public Entity Entity;
    public TransformComponent Transform;
}
